package com.example.controller;

import com.example.common.BusinessError;
import com.example.common.CommonError;
import com.example.common.CommonRes;
import com.example.dao.Permission;
import com.example.service.PermissionService;
import com.example.validation.DtoValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 * 权限控制器，权限及目录
 * @author: jimmy
 * @date: 2023年12月31日 10:33
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    /**
     * 目录service
     */
    @Autowired
    private PermissionService permissionService;

    /**
     * 校验器
     */
    @Autowired
    private DtoValidator dtoValidator;

    /**
     * 获取所有目录
     * @return
     */
    @GetMapping("/getAllPermission")
    public CommonRes getAllPermission() {
        //sql里有排序，无需排序
        List<Permission> permissionList = permissionService.findAllPermissions();
        //一级目录
        List<Permission> permissionFirst = new ArrayList<>();
        //二级目录
        List<Permission> permissionSecond = new ArrayList<>();

        for (Permission permission : permissionList) {
            if (permission.getPid() == null) {
                permissionFirst.add(permission);
            } else {
                permission.setChildren(null);
                permissionSecond.add(permission);
            }
        }


        for (Permission permission : permissionFirst) {
            for (Permission second : permissionSecond) {
                if (second.getPid() == permission.getId()) {
                    permission.getChildren().add(second);
                }
            }
        }

        return CommonRes.create(permissionFirst);
    }


    /**
     * 根据目录id查询角色，检查目录是否被角色设置了
     *
     * @param id
     * @return
     */
    @GetMapping("/findRoleCountByPermissionId")
    public CommonRes findRoleCountByPermissionId(@RequestParam Long id) {
        int result = permissionService.findRoleCountByPermissionId(id);
        return CommonRes.create(result);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public CommonRes delete(@RequestBody Long[] ids) {
        int result = permissionService.deletePermissionById(ids[0]);
        if (result > 0) {
            return CommonRes.create(null);
        } else {
            CommonError commonError = new CommonError(BusinessError.UNKNOWN_ERROR);
            return CommonRes.createError(commonError);
        }
    }

    /**
     * 新增目录
     *
     * @param permission
     * @return
     */
    @PostMapping(value = "/addPermission")
    public CommonRes addPermission(@Valid @RequestBody Permission permission) {
        // com.example.aspect.ValidateAspect
        //通过aop校验传过来的用户实体类userDto

        //id是autoincrease
        permission.setId(null);
        //一级目录id为null
        if (permission.getPid() == 0) {
            permission.setPid(null);
        }
        int result = permissionService.insertPermission(permission);
        if (result > 0) {
            return CommonRes.create(null);
        } else {
            CommonError commonError = new CommonError(BusinessError.UNKNOWN_ERROR);
            return CommonRes.createError(commonError);
        }
    }


    /**
     * 修改目录
     *
     * @param permission
     * @return
     */
    @PostMapping(value = "/modifyPermission")
    public CommonRes modifyPermission(@Valid  @RequestBody Permission permission) {
        // com.example.aspect.ValidateAspect
        //通过aop校验传过来的用户实体类userDto
        int result = permissionService.updatePermission(permission);
        if (result > 0) {
            return CommonRes.create(null);
        } else {
            CommonError commonError = new CommonError(BusinessError.UNKNOWN_ERROR);
            return CommonRes.createError(commonError);
        }
    }

    /**
     * 校验目录名称，目录名称不能重复
     *
     * @param permissionname
     * @param id
     * @return
     */
    @GetMapping("/validatePermissionname")
    public CommonRes validatePermissionname(@RequestParam String permissionname, @RequestParam Long id) {
        //如果id不为空，则排除id对应的permission
        if (permissionService.isPermissionnameExisted(permissionname, id)) {
            return CommonRes.createError(null);
        } else {
            return CommonRes.create(null);
        }
    }


}
